<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MedicinalController extends Controller
{
    //药材管理首页
    public function getIndex(Request $request)
    {
        //查询所有的内容
        $res = DB::table('medicinal') -> paginate(10);

        $count = 1;
        //判断是否有搜索
        if($request -> input('keywords')){
            $res = DB::table('medicinal')
                ->where('name','like','%'.$request->input('keywords').'%')
                ->paginate(10);
        }else{
             //查询所有的内容
            $res = DB::table('medicinal') -> paginate(10);  
        }
        //获取所有的信息
        $data = $request -> all();
    	return view('Admin.medicinal.index',['medicinals' => $res,'count' => $count,'data' => $data]);
    }

    //添加药材页面
    public function getAdd()
    {
    	return view('Admin.medicinal.add');
    }

    //执行药材添加
    public function postInsert(Request $request)
    {

        // dd($request -> all());
        $data = $request -> except(['_token','mimg']);
        $data['ctime'] = time();
        // dd($data);
        // $data['mimg'] = $this -> upload($request,'mimg');
        $data['mimg'] = $this -> upload($request,'mimg');
        // dd($data);
        //执行上传
        $res = DB::table('medicinal') -> insert($data);
        //判断是否添加成功
        if($res){
            return redirect('/admin/medicinal/index') -> with('success','药材添加成功');
        }else{
            return back() -> with('error','药材添加失败');
        }
    }

    //药材图片上传的处理方法
    public function upload($request,$name)
    {
        // dd($name);
        if($request -> hasFile($name)){
            //随机图片名
            $n = md5(rand(1000,9999).time());
            //获取上传的后缀名
            $suffix = $request -> file($name) -> getClientOriginalExtension();
            //限制上传文件的后缀名类型
            $arr = array('jpg','png','jpeg','gif');
            //判断
            if(!in_array($suffix,$arr)){
                die('上传文件后缀名不符合要求');
            }
            //移动文件
            $request -> file($name) -> move('./bootcss/admin/img/medicinal/' , $n . '.' . $suffix);
            //需要在文件上传成功后返回一个路径,方便上传数据库
            return '/bootcss/admi/img/medicinal/' . $n . '.' . $suffix;
        }
    }

    //药材信息修改页面
    public function getEdit($id)
    {
        //获取信息
        $res = DB::table('medicinal') -> where('id',$id) -> first();
        // dd($res);

    	return view('Admin.medicinal.edit',['ms' => $res]);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
        //获取信息
        $id = $request -> input('mid');
        // dd($id);
        $data = $request -> except(['_token','mid','mimg']);
        $data['mimg'] = $this -> upload($request,'mimg');
        // dd($data);
        //执行修改
        $res = DB::table('medicinal') -> where('id',$id) -> update($data);
        // dd($res);
        //判断
        if($res){
            return redirect('/admin/medicinal/index') -> with('success','药材信息修改成功!');
        }else{ 
            return back() -> with('error','药材信息修改失败');
        }
    }

    //删除某条信息
    public function getDelete($id)
    {
        //执行删除
        $res = DB::table('medicinal') -> where('id',$id) -> delete();
        //判断
        if($res){
            return redirect('/admin/medicinal/index') -> with('success','删除成功');
        }else{
            return back() -> width('erroc','删除失败');
        }

    }
}
